/**
  ******************************************************************************
  * @file    gt32f030_iwdg.c
  * @author  GT Application Team
  * @version V1.0.0
  * @date    03-January-2025      
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2022 Giantec Semicondutor Inc</center></h2>
  *
  *             http://www.giantec-semi.com/
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "gt32f030_rcc.h"
#include "gt32f030_iwdg.h"


/** @addtogroup GT32F030_StdPeriph_Driver
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

#define IWDG_FLAG_RUNNING   ((uint32_t)0x00000004)
#define IWDG_IT_Overflow    ((uint32_t)0x00000001)

/** @defgroup IWDG 
  * @brief IWDG driver modules
  * @{
  */ 

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup IWDG_Private_Functions
  * @{
  */ 

/** @defgroup IWDG_Init Initialization functions
 *  @brief   Initialization and functions
 *
@verbatim    
 ===============================================================================
              ##### Initialization functions #####
 ===============================================================================
@endverbatim
  * @{
  */
void IWDG_Init(IWDG_InitTypeDef* IWDG_InitStruct)
{
	/* Check the parameters */
  assert_param(IS_IWDG_RELOAD(IWDG_InitStruct->IWDG_ReloadVal));
  assert_param(IS_IWDG_INTMASK(IWDG_InitStruct->IWDG_INTMask)); 
  assert_param(IS_IWDG_MODE(IWDG_InitStruct->IWDG_Mode));
 
  /*---------------------------- IWDG CFG & RLOAD Configuration -----------------*/
  IWDG->CR   = IWDG_InitStruct->IWDG_INTMask|IWDG_InitStruct->IWDG_ActiveMode;
	IWDG->RLOAD = IWDG_InitStruct->IWDG_ReloadVal; 
}

/**
  * @brief  Fills each IWDG_InitStruct member with its default value.
  * @param  IWDG_InitStruct: pointer to a IWDG_InitTypeDef structure
  *         which will be initialized.
  * @retval None
  */
void IWDG_StructInit(IWDG_InitTypeDef* IWDG_InitStruct)
{
		/* Check the parameters */
	
	IWDG_InitStruct->IWDG_ReloadVal   = 0xFFFFF;
	IWDG_InitStruct->IWDG_INTMask     = IWDG_INTMask_Enable;
	IWDG_InitStruct->IWDG_ActiveMode  = IWDG_ActiveMode_Reset;
}

/**
  * @}  
  */





/** @defgroup IWDG_OpCfg Operation and Configuration functions
 *  @brief   Operation and Configuration functions
 *
@verbatim   
 ===============================================================================
           ##### Operation and Configuration functions #####
 ===============================================================================  
@endverbatim
  * @{
  */
/**
  * @brief  Unlocks the IWDG peripheral Registers access.
  * @param  None
  * @retval None
  */
void IWDG_Unlock(void)
{
	IWDG->UNLOCK    = 0x55aa6699;
}

/**
  * @brief  Locks the IWDG peripheral Registers access.
  * @param  None
  * @retval None
  */
void IWDG_Lock(void)
{
	IWDG->UNLOCK    = 0;
}

/**
  * @brief  Set the IWDG Execute Command.
  * @param  ExeCmd: the IWDG Execute Command
  * @retval None
  */
void IWDG_SetExecuteCommand(unsigned int ExeCmd)
{
	/* Check the parameters */
  assert_param(IS_IWDG_CMD(ExeCmd));
	IWDG->UNLOCK    = 0x55aa6699;
  IWDG->CMD = ExeCmd;
	IWDG->UNLOCK    = 0;
}

/**
  * @brief  Set the IWDG Reload value.
	* @param  Reload: the IWDG Reload value.
  * @retval None
  */
void IWDG_SetReloadVal(unsigned int Reload)
{
	/* Check the parameters */
  assert_param(IS_IWDG_RELOAD(Reload));
  IWDG->RLOAD = Reload;
}

/**
  * @brief  Get the IWDG Reload value.
  * @param  None
  * @retval the IWDG Reload value.
  */
unsigned int IWDG_GetCountVal(void)
{
  return IWDG->CNT;
}

/**
  * @}  
  */



/** @defgroup  IWDG_IntrFlag Interrupts and flags management functions
 *  @brief    Interrupts and flags management functions
 *
@verbatim   
 ===============================================================================
           #####  Interrupts and flags management functions #####
 ===============================================================================  
@endverbatim
  * @{
  */
/**
  * @brief  Checks whether the IWDG interrupt flag is set or not.
  * @param  None
  * @retval The new state of IWDG interrupt flag(SET or RESET).
  */
ITStatus IWDG_GetIT(void)
{
  ITStatus bitstatus = RESET;
  if (IWDG->INTSR&IWDG_IT_Overflow)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }
  return  bitstatus;
}

/**
  * @brief  Clears the IWDG interrupt flag.
  * @param  None
  * @retval None
  */
void IWDG_ClearIT(void)
{
	IWDG->UNLOCK    = 0x55aa6699;
  IWDG->INTCLR  = 0x1;
	IWDG->UNLOCK    = 0; 
}

/**
  * @brief  Get the Running Status flag.
  * @param  None
  * @retval The new state of Running Status(SET or RESET).
  */
FlagStatus IWDG_GetRunningStatus(void)
{
  FlagStatus bitstatus = RESET;

  if (IWDG->CR & IWDG_FLAG_RUNNING)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }
  return  bitstatus;
}
/**
  * @}  
  */ 

/**
  * @}  
  */

/**
  * @}  
  */ 

/**
  * @}
  */ 
/************************ (C) COPYRIGHT Giantec Semicondutor Inc *****END OF FILE****/
